﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace WindowsGame1.Objects3D
{
    class clsCube
    {
        private GraphicsDevice device;

        //  Modelo 3D
        public static Model model;
        public int X, Y;

        //  Matrizes do modelo
        public Matrix world;  //  Diminui o cubo e o posiciona o cubo "sobre" o plano
        
        public clsCube(GraphicsDevice graphicsDevice, int x, int y, Vector3 position, Vector3 size)
        {
            device = graphicsDevice;
            X = x;
            Y = y;
            //Vector3.Multiply(position, size);
            world = Matrix.CreateTranslation(position) /* Matrix.CreateScale(size)*/;
        }

        public void LoadContent(ContentManager content)
        {
            //  Load a model
            model = content.Load<Model>(@"cube");

            //  Calculates the effect aspect ratio, projection and view matrix
            float aspectRatio = (float)device.Viewport.Width / device.Viewport.Height;
        }


        public void Draw()
        {
            //  Draw the model
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = world;
                    effect.View = Camera.View;
                    effect.Projection = Camera.Projection;

                    effect.EnableDefaultLighting();
                }
                // Draw each mesh of the model
                mesh.Draw();
            }
        }

        public void Update()
        {
            //  Move o cubo
            KeyboardState currentKeyboardState = Keyboard.GetState();
            //  Setas direita e esquerda movem cubo em X
            if (currentKeyboardState.IsKeyDown(Keys.Left))
                world *= Matrix.CreateTranslation(new Vector3(-0.1f, 0f, 0f));
            if (currentKeyboardState.IsKeyDown(Keys.Right))
                world *= Matrix.CreateTranslation(new Vector3(+0.1f, 0f, 0f));

            //  Setas cima e baixo movem cubo em Z
            if (currentKeyboardState.IsKeyDown(Keys.Up))
                world *= Matrix.CreateTranslation(new Vector3(0f, 0f, -0.1f));
            if (currentKeyboardState.IsKeyDown(Keys.Down))
                world *= Matrix.CreateTranslation(new Vector3(0f, 0f, 0.1f));

            //Page-Up e Page-down movem cubo em Y
            if (currentKeyboardState.IsKeyDown(Keys.PageDown))
                world *= Matrix.CreateTranslation(new Vector3(0f, -0.1f, 0f));
            if (currentKeyboardState.IsKeyDown(Keys.PageUp))
                world *= Matrix.CreateTranslation(new Vector3(0f, 0.1f, 0f));

            //  Roda nos eixos Y e X e Z com os números 1, 2 e 3 do teclado numérico
            if (currentKeyboardState.IsKeyDown(Keys.NumPad1))
                world *= Matrix.CreateRotationY(0.01f);
            if (currentKeyboardState.IsKeyDown(Keys.NumPad2))
                world *= Matrix.CreateRotationX(0.01f);
            if (currentKeyboardState.IsKeyDown(Keys.NumPad3))
                world *= Matrix.CreateRotationZ(0.01f);
        }
    }
}